package pl.edu.utp.kursy;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import pl.edu.utp.kursy.database.Company;
import pl.edu.utp.kursy.database.DatabaseManager;
import pl.edu.utp.kursy.database.Worker;

public class WorkerInfoPanel extends javax.swing.JPanel {

    private Worker worker;
    private ArrayList<Company> companies;
    
    public WorkerInfoPanel(Worker worker) {
        this.worker = worker;
        initComponents();
        
        DatabaseManager dataManager = new DatabaseManager();
        companies = dataManager.getAllCompanies();
        
        setWorkerFields();
        setEditableMode(false);
        showCompanyChooser(false);
    }

    public Worker getWorker() {
        
        if(validateFields()){
            if(worker == null){
                worker = new Worker();
            }
            worker.setDocumentNumber(documentNumberTextField.getText());
            worker.setEmail(emailTextField.getText());
            worker.setName(nameTextField.getText());
            worker.setPesel(peselTextField.getText());
            worker.setPhone(phoneTextField.getText());
            worker.setSurname(surnameTextField.getText());

            if(companyComboBox.isVisible()){
                Company selectedCompany = (Company)companyComboBox.getSelectedItem();
                if(selectedCompany != null){
                    worker.setCompany(selectedCompany);
                }
            }
            return worker;
        }
        
        return null;
    }
    
    private boolean validateFields(){
        
        String documentNumber = documentNumberTextField.getText();
        String email = emailTextField.getText();
        String name = nameTextField.getText();
        String surname = surnameTextField.getText();
        String pesel = peselTextField.getText();
        String phone = phoneTextField.getText();
        
        if(name.isEmpty()){
            JOptionPane.showMessageDialog(this, getString("worker_name_empty_message"),
                                                getString("worker_name_empty_title"),
                                                JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if(surname.isEmpty()){
            JOptionPane.showMessageDialog(this, getString("worker_surname_empty_message"),
                                                getString("worker_surname_empty_title"),
                                                JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        //TODO Przemyśleć czy i jak walidować pozostałe pola
        
        return true;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
        setWorkerFields();
        setEditableMode(false);
    }
    
    private void setWorkerFields(){
        if(companies != null){
            for(Company company : companies){
                companyComboBox.addItem(company);
            }
            if(!companies.isEmpty()){
                companyComboBox.setSelectedIndex(0);
            }
        }
        if(worker != null){
            documentNumberTextField.setText(worker.getDocumentNumber());
            emailTextField.setText(worker.getEmail());
            nameTextField.setText(worker.getName());
            peselTextField.setText(worker.getPesel());
            phoneTextField.setText(worker.getPhone());
            surnameTextField.setText(worker.getSurname());
        }
    }
    
    public final void setEditableMode(boolean isEditable){
        documentNumberTextField.setEditable(isEditable);
        emailTextField.setEditable(isEditable);
        nameTextField.setEditable(isEditable);
        peselTextField.setEditable(isEditable);
        phoneTextField.setEditable(isEditable);
        surnameTextField.setEditable(isEditable);
    }
    
    public final void showCompanyChooser(boolean show){
        companyLabel.setVisible(show);
        companyComboBox.setVisible(show);
    }
    
    protected String getString(String key){
        return StringsProperties.getString(key);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        workerDetailsPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        surnameLabel = new javax.swing.JLabel();
        peselLabel = new javax.swing.JLabel();
        documentNumberLabel = new javax.swing.JLabel();
        phoneLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        surnameTextField = new javax.swing.JTextField();
        peselTextField = new javax.swing.JTextField();
        documentNumberTextField = new javax.swing.JTextField();
        phoneTextField = new javax.swing.JTextField();
        emailTextField = new javax.swing.JTextField();
        companyLabel = new javax.swing.JLabel();
        companyComboBox = new javax.swing.JComboBox();

        setLayout(new java.awt.BorderLayout());

        workerDetailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getString("worker_details")));

        nameLabel.setText(getString("worker_name"));

        nameTextField.setAutoscrolls(false);

        surnameLabel.setText(getString("worker_surname"));

        peselLabel.setText(getString("worker_pesel"));

        documentNumberLabel.setText(getString("worker_document_number"));

        phoneLabel.setText(getString("worker_phone"));

        emailLabel.setText(getString("worker_email"));

        companyLabel.setText(getString("company"));

        javax.swing.GroupLayout workerDetailsPanelLayout = new javax.swing.GroupLayout(workerDetailsPanel);
        workerDetailsPanel.setLayout(workerDetailsPanelLayout);
        workerDetailsPanelLayout.setHorizontalGroup(
            workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, workerDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(emailLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(phoneLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(documentNumberLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(peselLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(surnameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nameLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(emailTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(documentNumberTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(peselTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(surnameTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameTextField)
                    .addComponent(companyComboBox, 0, 228, Short.MAX_VALUE))
                .addContainerGap())
        );
        workerDetailsPanelLayout.setVerticalGroup(
            workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(workerDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(surnameLabel)
                    .addComponent(surnameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(peselLabel)
                    .addComponent(peselTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(documentNumberLabel)
                    .addComponent(documentNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phoneLabel)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLabel)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(workerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyLabel)
                    .addComponent(companyComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(workerDetailsPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox companyComboBox;
    private javax.swing.JLabel companyLabel;
    private javax.swing.JLabel documentNumberLabel;
    private javax.swing.JTextField documentNumberTextField;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel peselLabel;
    private javax.swing.JTextField peselTextField;
    private javax.swing.JLabel phoneLabel;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JLabel surnameLabel;
    private javax.swing.JTextField surnameTextField;
    private javax.swing.JPanel workerDetailsPanel;
    // End of variables declaration//GEN-END:variables
}
